package org.lambda.stream;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SteamDemo {

    private static final int SIZE = 10;

    public static void main(String[] args) {
        // map();
        list();
    }

    private static void list() {
        List<Integer> list = new ArrayList<Integer>(10) {
            private static final long serialVersionUID = -5305280823697849111L;

            {
                for (int i = 0; i < SIZE; i++) {
                    add(i);
                }
            }
        };

        Optional<Integer> min = list.stream().min(Comparator.comparing(Function.identity()));
        Optional<Integer> max = list.stream().max(Integer::compareTo);
        System.out.println(String.format("min:%s; max:%s", min.get(), max.get()));

        Optional<Integer> aa = list.stream().filter(str -> !str.equals("3")).findFirst();
        Optional<Integer> bb = list.stream().filter(str -> !str.equals("3")).findAny();

        Optional<Integer> aa1 = list.parallelStream().filter(str -> !str.equals("3")).findFirst();
        Optional<Integer> bb1 = list.parallelStream().filter(str -> !str.equals("3")).findAny();

        System.out.println(aa.get() + "===" + bb.get());
        System.out.println(aa1.get() + "===" + bb1.get());

        Optional<Integer> aa2 = list.stream().filter(str -> !str.equals("3")).parallel().findFirst();
        Optional<Integer> bb2 = list.stream().filter(str -> !str.equals("3")).parallel().findAny();

        Optional<Integer> aa3 = list.stream().filter(str -> !str.equals("3")).sequential().findFirst();
        Optional<Integer> bb3 = list.stream().filter(str -> !str.equals("3")).sequential().findAny();

        System.out.println(aa2.get() + "===" + bb2.get());
        System.out.println(aa3.get() + "===" + bb3.get());

        Stream<String> stream = Stream.of("I", "love", "you", "too");
        /* 方式１ */
        List<String> list1 = stream.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
        System.out.println(list1);
        /* 方式2 */
        List<String> list2 = stream.collect(Collectors.toList());
        System.out.println(list2);
    }
}
